package com.jiangxjsy.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jiangxjsy.admin.mapper.AdminDeptMapper;
import com.jiangxjsy.admin.service.AdminDeptService;
import com.jiangxjsy.core.common.BaseStatus;
import com.jiangxjsy.core.exception.BasicException;
import com.jiangxjsy.core.util.RecursionUtil;
import com.jiangxjsy.core.wrap.ResultCode;
import com.jiangxjsy.model.admin.bo.AdminDeptBO;
import com.jiangxjsy.model.admin.bo.AdminDeptQueryBO;
import com.jiangxjsy.model.admin.entity.AdminDept;
import com.jiangxjsy.model.admin.vo.AdminDeptVO;
import com.jiangxjsy.model.admin.vo.DeptVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/6 16:29
 */

@Service
public class AdminDeptServiceImpl implements AdminDeptService {
    @Resource
    private AdminDeptMapper adminDeptMapper;

    /**
     * 根据部门 id 查询部门名称
     *
     * @param deptId 部门 id
     * @return 部门名称
     */
    @Override
    public String getNameByDeptId(Long deptId) {
        AdminDept adminDept = adminDeptMapper.selectByDeptId(deptId);
        return adminDept != null ? adminDept.getName() : "";
    }

    /**
     * 查询部门树列表
     *
     * @param queryBO 查询参数
     * @return 部门树列表
     */
    @Override
    public List<AdminDeptVO> queryDeptTree(AdminDeptQueryBO queryBO) {
        if (queryBO.getId() == null) {
            queryBO.setId(0);
        }

        if ("tree".equals(queryBO.getType())) {
            List<AdminDeptBO> adminDeptList = adminDeptMapper.selectDeptList();
            return RecursionUtil.getChildListTree(adminDeptList, "pid", queryBO.getId(), "deptId", "children", AdminDeptVO.class);
        }

        List<AdminDept> adminDeptList = adminDeptMapper.selectAll();
        if ("update".equals(queryBO.getType())) {
            List<Integer> ids = RecursionUtil.getChildList(adminDeptList, "pid", queryBO.getId(), "deptId", "deptId");
            adminDeptList.removeIf(dept -> ids.contains(Math.toIntExact(dept.getDeptId())));
        }

        return adminDeptList.stream().map(obj -> BeanUtil.copyProperties(obj, AdminDeptVO.class)).collect(Collectors.toList());
    }

    /**
     * 查询部门下属部门
     * @param parentId 上级部门 id
     * @return 下属部门 id 列表
     */
    @Override
    public List<Integer> queryChildDept(Integer parentId) {
        return RecursionUtil.getChildList(adminDeptMapper.selectAll(), "pid", parentId, "deptId", "deptId");
    }

    /**
     * 根据部门 id 查询部门信息
     *
     * @param deptId 部门 id
     * @return 部门信息
     */
    @Override
    public AdminDept queryByDeptId(Long deptId) {
        return adminDeptMapper.selectByDeptId(deptId);
    }

    /**
     * 查询部门的用户列表
     *
     * @return 部门的用户列表
     */
    @Override
    public List<DeptVO> queryDeptUserList() {
        return adminDeptMapper.selectDeptUserList();
    }

    /**
     * 新增部门
     *
     * @param adminDeptBO 部门信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDept(AdminDeptBO adminDeptBO) {
        if (BaseStatus.CLOSE.getStatus().equals(Math.toIntExact(adminDeptBO.getPid()))) {
            throw new BasicException(ResultCode.SystemResultCode.SYSTEM_NO_VALID);
        }
        Integer ignore = adminDeptMapper.insertAdminDept(
                BeanUtil.copyProperties(adminDeptBO, AdminDept.class)
        );
    }
}
